package com.lanou.zy.projecta.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.World;
import com.lanou.zy.projecta.util.ProgressView.CircleProgress;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class DestinationFragment extends Fragment implements View.OnClickListener{
    private View view;
    private Button asia_Btn, europe_Btn, northAmerica_Btn, africa_Btn, antarctica_Btn, southAmerica_Btn, oceania_Btn;
    private RequestQueue queue;
    private int black, white;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_destination, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        asia_Btn = (Button) view.findViewById(R.id.asia_Btn);
        asia_Btn.setOnClickListener(this);
        europe_Btn = (Button) view.findViewById(R.id.europe_Btn);
        europe_Btn.setOnClickListener(this);
        northAmerica_Btn = (Button) view.findViewById(R.id.northAmerica_Btn);
        northAmerica_Btn.setOnClickListener(this);
        africa_Btn = (Button) view.findViewById(R.id.africa_Btn);
        africa_Btn.setOnClickListener(this);
        antarctica_Btn = (Button) view.findViewById(R.id.antarctica_Btn);
        antarctica_Btn.setOnClickListener(this);
        southAmerica_Btn = (Button) view.findViewById(R.id.southAmerica_Btn);
        southAmerica_Btn.setOnClickListener(this);
        oceania_Btn = (Button) view.findViewById(R.id.oceania_Btn);
        oceania_Btn.setOnClickListener(this);
        initData(10);
        asia_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.asia_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                initData(10);
                break;

            case R.id.europe_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                initData(12);
                break;

            case R.id.northAmerica_Btn:
                initData(234);
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                break;

            case R.id.africa_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                initData(76);
                break;

            case R.id.antarctica_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                initData(759);
                break;

            case R.id.southAmerica_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent);
                initData(235);
                break;

            case R.id.oceania_Btn:
                asia_Btn.setBackgroundResource(R.mipmap.seven_continent);
                europe_Btn.setBackgroundResource(R.mipmap.seven_continent);
                northAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                africa_Btn.setBackgroundResource(R.mipmap.seven_continent);
                antarctica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                southAmerica_Btn.setBackgroundResource(R.mipmap.seven_continent);
                oceania_Btn.setBackgroundResource(R.mipmap.seven_continent_pressed);
//                asia_Btn.setTextColor(black);
//                europe_Btn.setTextColor(black);
//                northAmerica_Btn.setTextColor(black);
//                africa_Btn.setTextColor(black);
//                antarctica_Btn.setTextColor(black);
//                southAmerica_Btn.setTextColor(black);
//                oceania_Btn.setTextColor(white);
                initData(239);
                break;
        }
    }

    private void initData(final int id) {
        queue = Volley.newRequestQueue(getActivity());
        String url = "http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=868652011023578&track_app_version=6.6&track_app_channel=360m&track_device_info=amoi77_cu_ics2&track_os=Android4.0.4&app_installtime=1441094124336&lat=38.884021&lon=121.544588";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                List<World.DataEntity> wd;
                World world = gson.fromJson(response, World.class);
                wd = world.getData();
                for (World.DataEntity d : wd){

                    if (d.getId() == id) {
                        List<World.DataEntity.HotCountryEntity> wdh = d.getHot_country();

                        Bundle bundle = new Bundle();
                        bundle.putSerializable("list", (Serializable) wdh);

                        bundle.putString("id", d.getCnname());
                    }
                }
                FragmentManager Manager = getChildFragmentManager();
                if (id == 10) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(0).getHot_country(), wd.get(0).getCnname())).commitAllowingStateLoss();
                } else if (id == 12) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(1).getHot_country(), wd.get(1).getCnname())).commitAllowingStateLoss();
                } else if (id == 234) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(2).getHot_country(), wd.get(2).getCnname())).commitAllowingStateLoss();
                } else if (id == 235) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(3).getHot_country(), wd.get(3).getCnname())).commitAllowingStateLoss();
                } else if (id == 239) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(4).getHot_country(), wd.get(4).getCnname())).commitAllowingStateLoss();
                } else if (id == 76) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(5).getHot_country(), wd.get(5).getCnname())).commitAllowingStateLoss();
                } else if (id == 759) {
                    Manager.beginTransaction().replace(R.id.hotDestination, HotCountryFragment.newInstance(wd.get(6).getHot_country(), wd.get(6).getCnname())).commitAllowingStateLoss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }
}
